//
//  SettingsViewController.m
//  CalculatorApp
//
//  Created by Michael Geering on 29/03/2010.
//  Copyright 2010 SDC. All rights reserved.
//

#import "SettingsViewController.h"


@implementation SettingsViewController

@synthesize delegate, randomButton, randomLabel, randomSlider, messageLabel, messageSlider, messageButton, imageView;


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewWillAppear:(BOOL)animated {
	if (![[NSFileManager defaultManager] fileExistsAtPath:[self dataFilePath]]) {
		[messageButton setOn:YES];
		[randomButton setOn:YES];
		[randomSlider setValue:10.0f];
		[messageSlider setValue:4.0f];
		[randomLabel setText:@"10"];
		[messageLabel setText:@"4"];
		kRandomValue = 10;
		kMessageValue = 4;
		[[NSFileManager defaultManager] createFileAtPath:[self dataFilePath] contents:nil attributes:nil];
		[self setContentsOfPlist];

	} else {
		[self getContentsOfPlist];
	}
	imageView.alpha = 0.0;
	imageView.hidden = YES;
    [super viewDidLoad];
	self.view.backgroundColor = [UIColor grayColor];

	
}

- (IBAction)showMainView {
	[self setContentsOfPlist];
	[self.delegate settingsViewControllerDidFinish:self];
}
- (IBAction)messageSliderValueChanged:(id)sender {
	
	[messageLabel setText:[NSString stringWithFormat:@"%d", (int)messageSlider.value]];
/*	UISlider *slider = (UISlider *)sender;
	int value = (int)(slider.value);
	NSString *label = [[NSString alloc] initWithFormat:@"%d%%", value];
	messageLabel.text = label; */
	
	kMessageValue = (int)messageSlider.value; 
//	[label release];
	
}
- (IBAction)randomSliderValueChanged:(id)sender {
	UISlider *slider = (UISlider *)sender;
	int value = (int)(slider.value);
	NSString *label = [[NSString alloc] initWithFormat:@"%d", value];
	randomLabel.text = label;
	kRandomValue = value;
	[label release];
}
- (IBAction)messageButtonStateChanged:(id)sender {
	UISwitch *button = (UISwitch *)sender;
	if (button.isOn) {
		messageButtonState = YES;
	} else {
		messageButtonState = NO;
	}
}
- (IBAction)randomButtonStateChanged:(id)sender {
	UISwitch *button = (UISwitch *)sender;
	if (button.isOn) {
		randomButtonState = YES;
	} else {
		randomButtonState = NO;
	}
}
- (NSString *)dataFilePath {
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSMutableString *directory = [NSMutableString stringWithString:[paths objectAtIndex:0]];
	[directory appendString:@"/settings.plist"];

	return directory;
} 
- (void)getContentsOfPlist {
	NSArray *plistArray = [[NSArray alloc] initWithContentsOfFile:[self dataFilePath]];
	
	kMessageValue = [[plistArray objectAtIndex:0] intValue];
	kRandomValue = [[plistArray objectAtIndex:1] intValue];
	messageButtonState = [[plistArray objectAtIndex:2] boolValue];
	randomButtonState = [[plistArray objectAtIndex:3] boolValue];
	
	[messageButton setOn:(BOOL)messageButtonState];
	[randomButton setOn:(BOOL)randomButtonState];
	[messageSlider setValue:(int)kMessageValue];
	[randomSlider setValue:(int)kRandomValue];
	[randomLabel setText:[NSString stringWithFormat:@"%d", (int)kRandomValue]];
	[messageLabel setText:[NSString stringWithFormat:@"%d", (int)kMessageValue]];
	[plistArray release];
}
- (void)setContentsOfPlist {
	NSMutableArray *plistArray = [[NSMutableArray alloc] init];
	[plistArray addObject:[NSNumber numberWithInteger:kMessageValue]];
	[plistArray addObject:[NSNumber numberWithInteger:kRandomValue]];
	if (messageButtonState) {
		[plistArray addObject:[NSNumber numberWithInteger:1]];
	}	else	{
		[plistArray addObject:[NSNumber numberWithInteger:0]];
	}
	if (randomButtonState) {
		[plistArray addObject:[NSNumber numberWithInteger:1]];
	}	else	{
		[plistArray addObject:[NSNumber numberWithInteger:0]];
	}
	if (![[NSFileManager defaultManager] fileExistsAtPath:[self dataFilePath]]) {
		[[NSFileManager defaultManager] createFileAtPath:[self dataFilePath] contents:nil attributes:nil];
	}
	[plistArray writeToFile:[self dataFilePath] atomically:YES];	
	[plistArray release];
}
- (IBAction)showCredits {
	imageView.hidden = NO;
	[UIView beginAnimations:@"showCreditView" context:nil];
	[UIView setAnimationDuration:1.0];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	[UIView setAnimationDelegate:self];
	imageView.alpha = 1.0;
	[UIView commitAnimations];
}
- (IBAction)releaseCredits {
	[UIView beginAnimations:@"hideCreditView" context:nil];
	[UIView setAnimationDuration:1.0];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	[UIView setAnimationDelegate:self];
	imageView.alpha = 0.0;
[UIView commitAnimations];
}

- (void)viewDidUnload {
	self.randomButton =
	self.randomLabel =
	self.randomSlider =
	self.messageLabel =
	self.messageSlider =
	self.messageButton =
	self.imageView =
	nil;
}


- (void)dealloc {
	[randomButton release];
	[randomLabel release];
	[randomSlider release];
	[messageLabel release];
	[messageSlider release];
	[messageButton release];
	[imageView release];
    [super dealloc];
}


@end
